#include "errorrequest.h"

#include "reply.h"

namespace b2gdns {

	namespace /*anonymous*/ {
	
		class ErrorReply : public Reply
		{
		public:
			ErrorReply(Header const& header,
				std::vector<std::string> const& question,
				ErrorRequest::Code error);

			virtual operator boost::asio::const_buffer();
		};
		
		ErrorReply::ErrorReply(Header const& header,
			std::vector<std::string> const& question,
			ErrorRequest::Code error)
			: Reply(header, question)
		{
			header_.qr = 1;
			header_.rcode = static_cast<char>(error);
			header_.anCount = 0;
			header_.nsCount = 0;
			header_.arCount = 0;
		}

		ErrorReply::operator boost::asio::const_buffer()
		{
			char buffer[max_udp_size];
			char *pb = buffer, *tmp = buffer;
			encode_header(tmp);
			encode_question(tmp);
			return boost::asio::const_buffer(pb, tmp - pb);
		}

	} // anonymous namespace

	ErrorRequest::ErrorRequest(Header const& header,
		std::vector<std::string> const& question, Code error)
		: Request(header, question), error_(error)
	{
	}

	std::unique_ptr<Reply> ErrorRequest::execute()
	{
		return std::unique_ptr<Reply>(new ErrorReply(
			header_, question_, error_));
	}

} // namespace b2gdns
